import { defineStore } from 'pinia'

export const useLoginStore = defineStore('login', {
	state: () => {
		return {
			token: null as string | null,
			userInfo: null as object | null
		}
	},
	actions: {
		login(response: any) {
			this.token = response.token
			delete response.token
			this.userInfo = response
			uni.setStorage({ key: 'token', data: this.token })
			uni.setStorage({ key: 'userInfo', data: JSON.stringify(this.userInfo) })
		},
		init() {
			const token = uni.getStorageSync('token')
			const userInfo = uni.getStorageSync('userInfo')
			if (token && userInfo) {
				this.userInfo = JSON.parse(userInfo)
				this.token = token
			}
		}
	}
})